#include "main.h"
#include "led.h"

void change_devstat(int dev, int stat)
{
	/*打开设备文件*/
	int fd;
	fd = open("/dev/led", O_RDWR);
	if (fd < 0) 
	{
		perror("open");
		return;
	}

	/*改变设备状态*/
	if (dev == 1)
	{
		if (stat == 1)
		{
			printf("led2_green on.\n");
			ioctl(fd,LED_ON,1);
		}
		else if (stat == 0)
		{
			printf("led2_green off.\n");
			ioctl(fd,LED_OFF,1);
		}
	}
	else if (dev == 2)
	{
		if (stat == 1)
		{
			printf("led2_orange on.\n");
			ioctl(fd,LED_ON,2);
		}
		else if (stat == 0)
		{
			printf("led2_orange off.\n");
			ioctl(fd,LED_OFF,2);
		}
	}

	/*关闭设备文件*/
	close(fd);
}

int login(int newfd,char* username,char* password)
{
	printf("client>>login\n");
	/*打印登录用户信息*/
	printf("username:%s\n",username);
	printf("password:%s\n",password);

	/*验证登录用户信息是否正确*/
	snprintf(sql,size,"select * from users \
		where username = '%s' and password = '%s';",username,password);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	/*返回验证结果*/
	if(row > 0)
	{
		write(newfd,"yes",3);
		printf("Client login success.\n");
		sleep(1);//等待客户端接收验证结果
		return 0;
	}
	else
	{
		write(newfd,"no",3);
		printf("Client login fail.\n");
		sleep(1);//等待客户端接收验证结果
		return 1;
	}
}

void regis(int newfd,char* username,char* password)
{
	printf("client>>register\n");

	/*验证用户是否已注册*/
	snprintf(sql,size,"select * from users \
		where username = '%s';",username);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	/*返回验证结果*/
	if(row > 0)
	{
		printf("Client register fail.\n");
		printf("User already exists.\n");
		write(newfd,"NO",2);
		return;
	}

	/*添加用户*/
	snprintf(sql,size,"insert into users values('%s','%s');",username,password);
	sqlite3_exec(pdb, sql, NULL, NULL, &errmsg);

	printf("Client register success!\n");
	write(newfd,"YES",3);
}

void control(int newfd,char* devname,char* devstat)
{
	int dev = -1, stat = -1;

	printf("client>>control\n");
	printf("控制指令:%s %s\n", devname, devstat);
	if (strncmp(devname, "led2_green", strlen(devname)) == 0) dev = 1;
	if (strncmp(devname, "led2_orange", strlen(devname)) == 0) dev = 2;
	if (strncmp(devstat, "on", strlen(devstat)) == 0) stat = 1;
	if (strncmp(devstat, "off", strlen(devstat)) == 0) stat = 0;

	if((dev == 1 || dev == 2) && (stat == 1 || stat == 0))
	{
		change_devstat(dev, stat);
		printf("Change device status success!\n");
		write(newfd,"SUCCESS",7);
	}
	else
	{
		printf("Change device status fail.\n");
		printf("Parameters wrong.\n");
		write(newfd,"FAIL",4);
	}
}

void req_paras(int newfd,char* username,char* password)
{
	printf("client>>request parameters\n");

	/*验证请求用户信息是否正确*/
	snprintf(sql,size,"select * from users \
		where username = '%s' and password = '%s';",username,password);
	sqlite3_get_table(pdb,sql, &result, &row, &col, &errmsg);

	/*返回验证结果*/
	if(row > 0)
	{
		write(newfd,"yes",3);
		printf("Access to get parameters!\n");
		sleep(1);//等待客户端接收验证结果
	}
	else
	{
		write(newfd,"no",3);
		printf("No access to get parameters.\n");
		sleep(1);//等待客户端接收验证结果
	}
}

void get_paras(int newfd)
{
	printf("client>>get parameters\n");

	pthread_mutex_lock(&mutex);//上锁
	write(newfd, mobilebuf, strlen(mobilebuf));
	pthread_mutex_unlock(&mutex);//释放锁
	
	printf("parameters sent to client: %s\n", mobilebuf);
}


